package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.HmzStorageRequestDetailMapper;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HmzStorageRequestMapper;

/**
 * 用于存储入库申请信息的Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-10
 */
@Service
public class HmzStorageRequestServiceImpl implements IHmzStorageRequestService {
    @Autowired
    private HmzStorageRequestMapper hmzStorageRequestMapper;
    @Autowired
    private IHmzStorageRequestDetailService hmzStorageRequestDetailService;
    @Autowired
    private IHmzProductService hmzProductService;
    @Autowired
    private IHmzProductTypeService hmzProductTypeService;
    @Autowired
    private IHmzWarehouseService hmzWarehouseService;

    /**
     * 查询用于存储入库申请信息的
     *
     * @param id 用于存储入库申请信息的主键
     * @return 用于存储入库申请信息的
     */
    @Override
    public HmzStorageRequest selectHmzStorageRequestById(Long id) {
        HmzStorageRequest hmzStorageRequest = hmzStorageRequestMapper.selectHmzStorageRequestById(id);
        //查询仓库
        Long warehouseId = hmzStorageRequest.getWarehouseId();
        HmzWarehouse hmzWarehouse = hmzWarehouseService.selectHmzWarehouseById(warehouseId);
        if(StringUtils.isNotNull(hmzWarehouse)){
            hmzStorageRequest.setWarehouseIdName(hmzWarehouse.getWarehouseName());
        }
        HmzStorageRequestDetail hmzStorageRequestDetail = new HmzStorageRequestDetail();
        hmzStorageRequestDetail.setStorageRequestId(id);
        //查询明细
        List<HmzStorageRequestDetail> hmzStorageRequestDetails = hmzStorageRequestDetailService.selectHmzStorageRequestDetailList(hmzStorageRequestDetail);
        if (hmzStorageRequestDetails != null && hmzStorageRequestDetails.size() > 0) {
            List<HmzProduct> arrayList = new ArrayList<>();
            for (HmzStorageRequestDetail storageRequestDetail : hmzStorageRequestDetails) {
                    Long productId = storageRequestDetail.getProductId();
                    HmzProduct hmzProduct = hmzProductService.selectHmzProductById(productId);
                Long productTypeId = hmzProduct.getProductTypeId();
                HmzProductType hmzProductType = hmzProductTypeService.selectHmzProductTypeById(productTypeId);
                if(StringUtils.isNotNull(hmzProductType)){
                    hmzProduct.setProductTypeIdName(hmzProductType.getProductTypeName());
                }
                arrayList.add(hmzProduct);
            }
            hmzStorageRequest.setInfoListS(arrayList);
//            hmzStorageRequest.setInfoList(hmzStorageRequestDetails);
        }
        return hmzStorageRequest;
    }

    /**
     * 查询用于存储入库申请信息的列表
     *
     * @param hmzStorageRequest 用于存储入库申请信息的
     * @return 用于存储入库申请信息的
     */
    @Override
    public List<HmzStorageRequest> selectHmzStorageRequestList(HmzStorageRequest hmzStorageRequest) {
        List<HmzStorageRequest> hmzStorageRequests = hmzStorageRequestMapper.selectHmzStorageRequestList(hmzStorageRequest);

        if (hmzStorageRequests != null && hmzStorageRequests.size() > 0) {

            for (HmzStorageRequest storageRequest : hmzStorageRequests) {
                //查询仓库
                Long warehouseId = storageRequest.getWarehouseId();
                HmzWarehouse hmzWarehouse = hmzWarehouseService.selectHmzWarehouseById(warehouseId);
                if(StringUtils.isNotNull(hmzWarehouse)){
                    storageRequest.setWarehouseIdName(hmzWarehouse.getWarehouseName());
                }
                HmzStorageRequestDetail hmzStorageRequestDetail = new HmzStorageRequestDetail();
                hmzStorageRequestDetail.setStorageRequestId(storageRequest.getId());
                //查询明细
                List<HmzStorageRequestDetail> hmzStorageRequestDetails = hmzStorageRequestDetailService.selectHmzStorageRequestDetailList(hmzStorageRequestDetail);
                if (hmzStorageRequestDetails != null && hmzStorageRequestDetails.size() > 0) {
//                    storageRequest.setInfoList(hmzStorageRequestDetails);
                    if(hmzStorageRequestDetails !=null && hmzStorageRequestDetails.size() > 0){
                        List<HmzProduct> arrayList = new ArrayList<>();
                        for (HmzStorageRequestDetail storageRequestDetail : hmzStorageRequestDetails) {
                            Long productId = storageRequestDetail.getProductId();
                            HmzProduct hmzProduct = hmzProductService.selectHmzProductById(productId);
                            Long productTypeId = hmzProduct.getProductTypeId();
                            if(StringUtils.isNotNull(productTypeId)){
                                HmzProductType hmzProductType = hmzProductTypeService.selectHmzProductTypeById(productTypeId);
                                if(StringUtils.isNotNull(hmzProductType)){
                                    hmzProduct.setProductTypeIdName(hmzProductType.getProductTypeName());
                                }
                            }

                            arrayList.add(hmzProduct);
                        }
                        storageRequest.setInfoListS(arrayList);
                    }

                }
            }
        }
        return hmzStorageRequests;
    }

    /**
     * 新增用于存储入库申请信息的
     *
     * @param hmzStorageRequest 用于存储入库申请信息的
     * @return 结果
     */
    @Override
    public int insertHmzStorageRequest(HmzStorageRequest hmzStorageRequest) {
        hmzStorageRequest.setCreateTime(DateUtils.getNowDate());
        int i = hmzStorageRequestMapper.insertHmzStorageRequest(hmzStorageRequest);
        //获取自增id
        Long id = hmzStorageRequest.getId();
        //获取入库单号
        String storageOrderNumber = hmzStorageRequest.getStorageOrderNumber();
        List<HmzStorageRequestDetail> infoList = hmzStorageRequest.getInfoList();
        if (infoList != null && infoList.size() > 0) {
            for (HmzStorageRequestDetail hmzStorageRequestDetail : infoList) {
                hmzStorageRequestDetail.setStorageRequestId(id);
                hmzStorageRequestDetail.setProductId(hmzStorageRequestDetail.getId());
                hmzStorageRequestDetail.setStorageOrderNumber(storageOrderNumber);
                hmzStorageRequestDetailService.insertHmzStorageRequestDetail(hmzStorageRequestDetail);
            }
        }
        return i;
    }

    /**
     * 修改用于存储入库申请信息的
     *
     * @param hmzStorageRequest 用于存储入库申请信息的
     * @return 结果
     */
    @Override
    public int updateHmzStorageRequest(HmzStorageRequest hmzStorageRequest) {
        hmzStorageRequest.setUpdateTime(DateUtils.getNowDate());
        return hmzStorageRequestMapper.updateHmzStorageRequest(hmzStorageRequest);
    }

    /**
     * 批量删除用于存储入库申请信息的
     *
     * @param ids 需要删除的用于存储入库申请信息的主键
     * @return 结果
     */
    @Override
    public int deleteHmzStorageRequestByIds(Long[] ids) {
        return hmzStorageRequestMapper.deleteHmzStorageRequestByIds(ids);
    }

    /**
     * 删除用于存储入库申请信息的信息
     *
     * @param id 用于存储入库申请信息的主键
     * @return 结果
     */
    @Override
    public int deleteHmzStorageRequestById(Long id) {
        return hmzStorageRequestMapper.deleteHmzStorageRequestById(id);
    }
}
